package com.xiahui.day10.Test3;

// 父类 (基类)
class Animal {
    String name;
    int age;

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void eat() {
        System.out.println("The animal is eating.");
    }

    public void sleep() {
        System.out.println("The animal is sleeping.");
    }
}

// 子类 (派生类)
class Dog extends Animal {
    String breed;

    public Dog(String name, int age, String breed) {
        super(name, age); // 调用父类的构造方法
        this.breed = breed;
    }

    public void bark() {
        System.out.println("Woof! Woof!");
    }

    @Override // 方法重写 (Override)
    public void eat() {
        System.out.println("The dog is eating dog food.");
    }

    public void displayInfo() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Breed: " + breed);
    }
}

public class InheritanceExample {
    public static void main(String[] args) {
        Dog dog = new Dog("Buddy", 3, "Golden Retriever");

        // 继承了父类的属性
        System.out.println("Name: " + dog.name);  // 输出：Name: Buddy
        System.out.println("Age: " + dog.age);    // 输出：Age: 3
        System.out.println("Breed: " + dog.breed); // 输出：Breed: Golden Retriever

        // 继承了父类的方法
        dog.eat();   // 输出：The dog is eating dog food. (重写后的方法)
        dog.sleep(); // 输出：The animal is sleeping.

        // 子类自己的方法
        dog.bark();  // 输出：Woof! Woof!
        dog.displayInfo(); // 输出 Buddy 的信息
    }
}

